﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;
using System.Windows.Forms;
using EIS.ClientRoutineJob;
using EIS.ClientUtility;
using EIS.Obj;
using EIS.ViewControl.Grid;
using Janus.Windows.ExplorerBar;

using Timer = System.Timers.Timer;

namespace EIS.Main
{

    public class RoutineJobScheduler
    {

        private static MainWindow parent = null;
        public static void ClientRoutineBatchStarter(MainWindow mainWindow)
        {
            parent = mainWindow;

            var timerHeartBeatRequest = new TimerWithParentControl(parent)
                                            {Interval = ClientConfig.Instance.HeartBeatInterval};
            timerHeartBeatRequest.Elapsed += HeartBeatRequest;
            timerHeartBeatRequest.Start();

            var timerFetchTransactionData = new TimerWithParentControl(parent)
                                            {Interval = ClientConfig.Instance.FetchTransactionDataInterval};

            timerFetchTransactionData.Elapsed += FetchTransactionData;
            timerFetchTransactionData.Start();

        }

        private static void HeartBeatRequest(Object source, ElapsedEventArgs e)
        {
            Timer timer = (Timer) source;
            timer.Enabled = false;
            var succ = new HeartBeat().HeartBeatRequest();
            if (succ == false)
            {
                var dialog = new ReconnectDialog();
                //dialog.SetToken(token);
                //dialog. = parent;
                dialog.ShowDialog();
            }

            timer.Enabled = true;
        }

        private static void FetchTransactionData(Object source, ElapsedEventArgs e)
        {
            Timer timer = null;

                if (source != null) timer = (Timer) source;
                var totalNum = 0;
                try
                {
                    if (source != null) timer.Enabled = false;
                    parent.SetStatusBar(true);
                    totalNum = new FetchTransactionData().Execute();
                    parent.SetStatusBar(false);
                    if (source != null) timer.Enabled = true;
                }
                catch (Exception ex)
                {
                    parent.SetStatusBar(false);
                    if (source != null) timer.Enabled = true;
                    Error.WriteEventLog("Error in data sync with server:" + ex.Message, ex.StackTrace);
                }
                if (totalNum != 0)
                {
                    SetNotifyIcon(totalNum + " new or updated objects");
                    if (GlobalVariable.Instance.CurrentViewHandler != null)
                    {
                        if (GlobalVariable.Instance.CurrentViewHandler.GetType().Name.Contains("ViewGridDisplay"))
                        {
                            ((ViewGridDisplay) GlobalVariable.Instance.CurrentViewHandler).GetLatestData();
                        }
                    }
                }
        }


        public static void ForceRefreshFromServer()
        {
            FetchTransactionData(null,null);
        }

        private static void SetNotifyIcon(String text)
        {
            string info = "New Sales Order Created: No. 12346 " + "\nNew Sales Order Created: No. 12346 " +
              "\nNew Sales Order Created: No. 12346 " + "\nNew Sales Order Created: No. 12346 " +
              "\nNew Sales Order Created: No. 12346 " + "\nNew Sales Order Created: No. 12346 " +
              "\nNew Sales Order Created: No. 12346 ";

            parent.SetNotifyIcon(text);
        }
    }
}
